package controller;

import dao.LoginDao;
import dao.UserDao;
import utils.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String tel = req.getParameter("tel");
        String address = req.getParameter("address");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setSex(sex);
        user.setTel(tel);
        user.setAddress(address);
        System.out.println(user);
        UserDao userDao = new UserDao();
        boolean tag = userDao.insert(user);
        LoginDao loginDao = new LoginDao();
        loginDao.insert(user);
        if(tag){
            System.out.println("注册成功...");
            req.getRequestDispatcher("/reg_success.html").forward(req,resp);
        }else {
            System.out.println("注册失败...");
        }
    }
}
